home *** CD-ROM | disk | FTP | other *** search
- /*
- * ODBCdbmsSetup.c -- ODBC Sample DBMS Setup Shared Library (or DLL).
- *
- * (c) Apple Computer, Inc 1993
- *
- * changes:
- *
- * 1.0a1 - removed all calls to OpenGlobalWorld, as this is compiled model far
- * and doesn't have any A5-relative references to globals. Also
- * removed code to set up the QuickDraw environment, e.g. calls to
- * InitGraf, etc., as these should not be done more than once from a process.
- * A5 world is assumed to have valid QD globals when this shared library is called.
- */
-
- #include <Dialogs.h>
- #include <Fonts.h>
- #if ! PPCODBC
- #include <LibraryManagerUtilities.h>
- #endif // ! PPCODBC
- #include <Menus.h>
- #include <OSUtils.h>
- #include <QuickDraw.h>
- #include <StdIO.h>
- #include <String.h>
- #include <TextEdit.h>
- #include <Windows.h>
-
- #include "DBMSsetup.h"
- #include "ODBCINST.H"
- #include "ODBCSharedLibraries.h"
-
- #include "SampleSetup.h"
-
- /*
- * data source keywords
- */
-
- LPCSTR kDefaultDataSourceName = "Default"; // default data source name
- LPCSTR kDataSourceNameKeyword = "DSN"; // data source name keyword
- LPCSTR kDescriptionKeyword = "Description"; // data source description keyword
- LPCSTR kOtherKeyword = "Other"; // other keyword
- LPCSTR kTranslateDLLKeyword = "TranslateDLL"; // translate DLL
- LPCSTR kTranslateKeyword = "Translate"; // translate keyword
- LPCSTR kTranslateOptionKeyword = "TranslateOption"; // translate option keyword
- LPCSTR kNoTranslation = "No Translation"; // no translation
- LPCSTR kEmptyString = ""; // empty string
- LPCSTR kODBCINI = "ODBC.INI";
-
- /*
- * prototypes
- */
-
- static INSTPREAPI BOOL INSTAPI DoConfigDSN (WindowPtr parentWindow, UINT fRequest, LPCSTR lpszDriver, LPCSTR lpszAttributes);
- static INSTPREAPI BOOL INSTAPI RemoveDataSource (WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info);
- static INSTPREAPI BOOL INSTAPI AddDataSource (WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info);
- static INSTPREAPI BOOL INSTAPI ConfigureDataSource (WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info);
- static INSTPREAPI BOOL INSTAPI ParseAttributes (LPCSTR lpszAttributes, DataSourceInfo *info);
- static INSTPREAPI BOOL INSTAPI GetKeywordValueFromAttributes (LPCSTR lpszKeyword, LPCSTR lpszAttributes, LPSTR lpszString);
-
- /*
- * public functions
- */
-
- #if PPCODBC
-
- #include <Memory.h>
- #include <Errors.h>
-
- Handle hinst;
-
- // _____________________________________________________________________________
- OSErr
- SampleSetupInit( InitBlockPtr ib )
- {
- /*
- * shared library init function. All shared libraries should have one. Its name is
- * declared in the .exp file.
- */
-
- if ( ib->fragLocator.where != kOnDiskFlat ) { return -32323; }
-
- hinst = NewHandle( sizeof( ODBCLibConnection ) );
- if ( ! hinst ) { return memFullErr; }
- HLock( hinst );
-
- (*(ODBCLibConnection**)hinst)->spec = *ib->fragLocator.u.onDisk.fileSpec;
- (*(ODBCLibConnection**)hinst)->connId = ib->connectionID;
-
- HUnlock( hinst );
-
- return noErr;
- }
-
-
- // _____________________________________________________________________________
- void
- SampleSetupMain( Handle inst2init )
- {
- HLock( hinst );
- HLock( inst2init );
- **(ODBCLibConnection**)inst2init = **(ODBCLibConnection**)hinst;
- HUnlock( hinst );
- HUnlock( inst2init );
- }
-
-
- #else // PPCODBC
-
- // _____________________________________________________________________________
- void
- SampleSetupInit()
- {
- /*
- * shared library init function. All shared libraries should have one. Its name is
- * declared in the .exp file.
- */
-
- // note: hinst is not needed for the 68k ODBCSharedLibraries functions
- }
-
- #endif // PPCODBC
-
- // _____________________________________________________________________________
- void
- SampleSetupObit()
- {
- /*
- * Shared library cleanup function. All shared libraries should have one. Its name is
- * declared in the .exp file.
- */
-
- #if PPCODBC
- DisposHandle( hinst );
- #endif // PPCODBC
- }
-
-
-
- INSTPREAPI BOOL INSTAPI
- ConfigDSN(HWND pGrafPort, /* window to center over */
- UINT fRequest, /* add, modify or delete */
- LPCSTR lpszDriver, /* name to present to user for this driver */
- LPCSTR lpszAttributes) /* buffer holding section data */
- {
- /*
- * this is the shared lilbrary entry point for configuring a data source.
- * ConfigDSN is responsible for adding, configuring and deleting a data source, and will
- * be called by the ODBC Configuration Manager. note that the even if we're adding
- * a new data source, a data source name keyword-value pair must be present in the attributes
- * buffer (i.e. "DSN=A Data Source Name"). in fact, when adding or deleting it can
- * be the only keyword-value pair in the attributes buffer.
- */
-
- BOOL success;
- ODBCLibResContext context;
-
- if ( ODBCLibUseLocalRes( &context ) )
- {
- SysBeep(1); // can't put up our alert if ODBCLibUseLocalRes failed
- return FALSE;
- }
-
- /*
- * here's where the work gets done
- */
-
- success = DoConfigDSN((WindowPtr) pGrafPort, fRequest, lpszDriver, lpszAttributes);
-
- /*
- * restore resource path, A5 world
- */
-
- ODBCLibCloseLocalRes( &context );
-
- return success;
- }
-
- /*
- * private functions
- */
-
- static INSTPREAPI BOOL INSTAPI
- DoConfigDSN(WindowPtr parentWindow, UINT fRequest, LPCSTR lpszDriver, LPCSTR lpszAttributes)
- {
- /*
- * pull out the keyword values from the attribute buffer, then add, configure or
- * remove the data source. note that the data source name must be one of the
- * keyword values in the buffer (DSN=Data Source Name), or nothing can be done.
- */
-
- BOOL success = false;
- DataSourceInfo info;
-
- /*
- * The attributes buffer should always contain the data source name - if
- * not ParseAttributes returns false and we deign to continue
- */
-
- if (!ParseAttributes(lpszAttributes, &info)) return false;
-
- /*
- * Add, Modify or Remove the data source
- */
-
- switch (fRequest)
- {
- case ODBC_ADD_DSN:
- success = AddDataSource(parentWindow, lpszDriver, &info);
- break;
-
- case ODBC_CONFIG_DSN:
- success = ConfigureDataSource(parentWindow, lpszDriver, &info);
- break;
-
- case ODBC_REMOVE_DSN:
- success = RemoveDataSource(parentWindow, lpszDriver, &info);
- break;
- }
-
- return success;
- }
-
- static INSTPREAPI BOOL INSTAPI
- RemoveDataSource(WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info)
- {
- /*
- * Call the config manager to remove the data source from the ODBC prefs file
- */
-
- return SQLRemoveDSNFromIni(info->name);
- }
-
- static INSTPREAPI BOOL INSTAPI
- AddDataSource(WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info)
- {
- /*
- * Call the setup dialog on the data source info, then call the config manager to
- * update the data source entry in the ODBC prefs file if the user clicked OK. Make
- * sure to remove the old data source entry as the user may have changed the name.
- */
- /*
- * Set the initial values for the new data source's keywords
- */
-
- strcpy(info->description, kEmptyString);
- strcpy(info->other, kEmptyString);
- strcpy(info->translateDLL, kEmptyString);
- strcpy(info->translate, kNoTranslation);
- strcpy(info->translateOption, kEmptyString);
-
- /*
- * Now let the user set the keyword values
- */
-
- if (!DoDBMSsetup(parentWindow, info))
- return false;
-
- /*
- * Now add the data source and its keywords. You might want to add check that a
- * datasource with this name doesn't already exist before writing…
- */
- if (!SQLWriteDSNToIni(info->name, lpszDriver)) return false;
-
-
- if (!SQLWritePrivateProfileString(info->name, kDescriptionKeyword, info->description, kODBCINI)) return false;
- if (!SQLWritePrivateProfileString(info->name, kOtherKeyword, info->other, kODBCINI)) return false;
- if (!SQLWritePrivateProfileString(info->name, kTranslateDLLKeyword, info->translateDLL, kODBCINI)) return false;
- if (!SQLWritePrivateProfileString(info->name, kTranslateKeyword, info->translate, kODBCINI)) return false;
- if (!SQLWritePrivateProfileString(info->name, kTranslateOptionKeyword, info->translateOption, kODBCINI)) return false;
-
- return true;
- }
-
- static INSTPREAPI BOOL INSTAPI
- ConfigureDataSource(WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info)
- {
- /*
- * Call the setup dialog on the data source info, then call the config manager to
- * update the data source entry in the ODBC prefs file if the user clicked OK. Make
- * sure to remove the old data source entry as the user may have changed the name.
- */
-
- char oldName[kMaxValueLength];
-
- strcpy(oldName, info->name);
-
- if (!DoDBMSsetup(parentWindow, info))
- return false;
-
- if (!SQLRemoveDSNFromIni(oldName))
- return false;
-
- if (!SQLWriteDSNToIni(info->name, lpszDriver))
- return false;
-
- if (!SQLWritePrivateProfileString(info->name, kDescriptionKeyword, info->description, kODBCINI)) return false;
- if (!SQLWritePrivateProfileString(info->name, kOtherKeyword, info->other, kODBCINI)) return false;
- if (!SQLWritePrivateProfileString(info->name, kTranslateDLLKeyword, info->translateDLL, kODBCINI)) return false;
- if (!SQLWritePrivateProfileString(info->name, kTranslateKeyword, info->translate, kODBCINI)) return false;
- if (!SQLWritePrivateProfileString(info->name, kTranslateOptionKeyword, info->translateOption, kODBCINI)) return false;
-
- return true;
- }
-
- static INSTPREAPI BOOL INSTAPI
- ParseAttributes(LPCSTR lpszAttributes, DataSourceInfo *info)
- {
- /*
- * Pull out the keyword values from the data source attributes buffer.
- * If the data source name keyword, DSN, isn't found, or is zero-length, then
- * return false. All other keyword values may be absent or zero-length.
- */
-
- if (!GetKeywordValueFromAttributes(kDataSourceNameKeyword, lpszAttributes, info->name))
- return false;
-
- GetKeywordValueFromAttributes(kDescriptionKeyword, lpszAttributes, info->description);
- GetKeywordValueFromAttributes(kOtherKeyword, lpszAttributes, info->other);
- GetKeywordValueFromAttributes(kTranslateDLLKeyword, lpszAttributes, info->translateDLL);
- GetKeywordValueFromAttributes(kTranslateKeyword, lpszAttributes, info->translate);
- GetKeywordValueFromAttributes(kTranslateOptionKeyword, lpszAttributes, info->translateOption);
-
- return true;
- }
-
- static INSTPREAPI BOOL INSTAPI
- GetKeywordValueFromAttributes(LPCSTR lpszKeyword, LPCSTR lpszAttributes, LPSTR lpszString)
- {
- /*
- * searches the attributes buffer (containing the packed array of keyword=value null-terminated
- * strings, with an additional null terminating the entire array) for the requested keyword,
- * and copies its value over to lpszString. lpszString should point to a buffer at least
- * kMaxValueLength bytes long. returns true if found and non-zero, false otherwise.
- */
-
- char *s, *t, *u;
- char tmpKeyword[kMaxKeywordLength];
-
- /*
- * loop through each keyword=value string in the buffer
- */
-
- for (lpszString[0] = 0, s = lpszAttributes; *s; s += strlen(s) + 1)
- {
- /*
- * pull out the keyword as a c-string
- */
-
- for (u = s, t = tmpKeyword; *u; u++, t++)
- {
- if (*u == '=')
- {
- u++;
- break;
- }
- *t = *u;
- }
- *t = 0;
-
- /*
- * if this is the desired keyword then copy its value to lpszString and beat it. u was
- * left pointing to the start of the value string from the previous operation
- */
-
- if (strcmp(tmpKeyword, lpszKeyword) == 0)
- {
- strcpy(lpszString, u);
- return true;
- }
- }
-
- return false;
- }
-